/*
 * @(#)MimirEntry.java	1.0 Apr 17, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.mimir;

import org.eoti.io.file.FileMonitor;
import org.eoti.io.file.FileUtil;
import org.eoti.io.file.event.FileChangedEvent;
import org.eoti.io.file.event.FileChangedListener;
import org.eoti.spec.mimirdb.v1.DBMapping;
import org.eoti.util.TimeUtil;

import java.io.File;
import java.io.PrintStream;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * This class represents a single entry in the database
 */
public class MimirEntry<DATA,ID>
		implements FileChangedListener
{
	protected static final String extension = ".xml";

	protected MimirScheme<DATA,ID> scheme;
	protected File entry;
	protected CopyOnWriteArrayList<EntryChangedListener<DATA,ID>> listeners;

	public MimirEntry(MimirScheme<DATA,ID> scheme, String id)
	{
		this(scheme, new File(scheme.getSchema().getParentFile(), id + extension));
	}

	public MimirEntry(MimirScheme<DATA,ID> scheme, File entry)
	{
		this.scheme = scheme;
		this.entry = entry;
		listeners = new CopyOnWriteArrayList<EntryChangedListener<DATA,ID>>();
	}

	public MimirScheme<DATA,ID> getScheme(){return scheme;}
	public File getFile(){return entry;}
	public MimirDB<DATA,ID> getDatabase(){return getScheme().getDatabase();}
	public MimirRegistry getRegistry(){return getScheme().getRegistry();}
	public DBMapping getMapping(){return getScheme().getMapping();}

	public void fileChanged(FileChangedEvent event)
	{
		// change to use FutureTask and ExecutorService?
		EntryChangedEvent<DATA,ID> newEvent = new EntryChangedEvent<DATA,ID>(this, event);
		for(EntryChangedListener<DATA,ID> listener : listeners)
			listener.entryChanged(newEvent);
	}

	public void addEntryChangedListener(EntryChangedListener<DATA,ID> listener)
	{
		listeners.add(listener);
		if(listeners.size() == 1)
			FileMonitor.addFileChangedListener(this, entry, 500, TimeUtil.Unit.Millisecond);
	}

	public void removeEntryChangedListener(EntryChangedListener<DATA,ID> listener)
	{
		listeners.remove(listener);
		if(listeners.size() == 0)
			FileMonitor.removeFileChangedListener(this, entry);
	}

	public boolean rename(String newId)
	{
		File newEntry = new File(entry.getParentFile(), newId+extension);
		return entry.renameTo(newEntry);
	}

	public Calendar lastModified()
	{
		long lastModified = entry.lastModified();
		if(lastModified == 0)
			return null;

		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date(lastModified));
		return cal;
	}

	public void touch()
	{
		FileUtil.touch(entry);
	}

	public DATA read()
			throws MimirException
	{
		return getDatabase().getContext().read(entry);
	}

	public void write(DATA data)
			throws MimirException
	{
		getDatabase().getContext().write(entry, data, getMapping());
	}

	public void display(PrintStream out)
			throws MimirException
	{
		getDatabase().getContext().display(entry, out, getMapping());
	}

	public void display()
			throws MimirException
	{
		getDatabase().getContext().display(entry, getMapping());
	}
}
